/*
 *
 *      Copyright (c) 2018-2025, exsecs All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: exsecs
 *
 */

package com.excesys.exsecs.admin.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.admin.api.dto.SysLoginLogDTO;
import com.excesys.exsecs.admin.api.entity.SysLog;
import com.excesys.exsecs.admin.api.entity.SysLoginLog;
import com.excesys.exsecs.admin.api.vo.PreLogVO;
import com.excesys.exsecs.admin.mapper.SysLoginLogMapper;
import com.excesys.exsecs.admin.service.SysLoginLogService;
import com.excesys.exsecs.common.core.constant.CommonConstants;
import com.excesys.exsecs.common.data.tenant.TenantBroker;
import com.excesys.exsecs.common.data.tenant.TenantContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 服务实现类
 * </p>
 *
 * @author exsecs
 * @since 2017-11-20
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

	/**
	 * 批量插入前端错误日志
	 * @param preLogVoList 日志信息
	 * @return true/false
	 */
	@Override
	public Boolean saveBatchLogs(List<PreLogVO> preLogVoList) {
		List<SysLoginLog> sysLoginLogs = preLogVoList.stream().map(pre -> {
			SysLoginLog log = new SysLoginLog();
			log.setLogType(CommonConstants.STATUS_LOCK);
			log.setTitle(pre.getInfo());
			log.setException(pre.getStack());
			log.setParams(pre.getMessage());
			log.setCreateTime(LocalDateTime.now());
			log.setRequestUri(pre.getUrl());
			log.setCreateBy(pre.getUser());
			return log;
		}).collect(Collectors.toList());
		return this.saveBatch(sysLoginLogs);
	}

	@Override
	public Page getLogByPage(Page page, SysLoginLogDTO sysLoginLog) {

		LambdaQueryWrapper<SysLoginLog> wrapper = Wrappers.lambdaQuery();
		if (StrUtil.isNotBlank(sysLoginLog.getLogType())) {
			wrapper.eq(SysLoginLog::getLogType, sysLoginLog.getLogType());
		}
		if (StrUtil.isNotBlank(sysLoginLog.getTitle())) {
			wrapper.like(SysLoginLog::getTitle, sysLoginLog.getTitle());
		}

		if (ArrayUtil.isNotEmpty(sysLoginLog.getCreateTime())) {
			wrapper.ge(SysLoginLog::getCreateTime, sysLoginLog.getCreateTime()[0]).le(SysLoginLog::getCreateTime,
					sysLoginLog.getCreateTime()[1]);
		}

		return baseMapper.selectPage(page, wrapper);
	}

	/**
	 * 插入日志
	 * @param SysLoginLog 日志对象
	 * @return true/false
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveLog(SysLoginLogDTO SysLoginLog) {
		TenantBroker.applyAs(SysLoginLog::getTenantId, tenantId -> {
			TenantContextHolder.setTenantId(tenantId);
			SysLoginLog log = new SysLoginLog();
			BeanUtils.copyProperties(SysLoginLog, log, "createTime");
			return baseMapper.insert(log);
		});
		return Boolean.TRUE;
	}

	/**
	 * sum 函数计算三十天内的数据
	 * @return list map
	 */
	@Override
	public List<Map<String, Object>> getLogSum() {
		return baseMapper.selectLogSumByType();
	}

}
